package com.hourses.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hourses.mapper.StoreMapper;
import com.hourses.page.Page;
import com.hourses.pojo.Result;
import com.hourses.pojo.Store;
import com.hourses.service.StoreService;
import com.hourses.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/1 17:49
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    //查询仓库列表
    @Autowired
    private StoreMapper storeMapper;
    @Override
    public Result selectAllStores() {
        //先看看缓存中有没有
        String redisStores = redisTemplate.opsForValue().get("stores");
        if (StringUtils.hasText(redisStores)){
            //缓存中有,直接返回
            return Result.ok(JSON.parseArray(redisStores));
        }
        //没有缓存的情况
        //先从数据库中去查
        List<Store> stores = storeMapper.selectAllStores();
        //查到以后将查询结果存入缓存当中
        redisTemplate.opsForValue().set("stores",JSON.toJSONString(stores));
        return Result.ok(stores);
    }

    //向前端ECharts框架传送图形中展示的数据
    @Override
    public List<StatisticsVo> selectEChartsData() {
        List<StatisticsVo> statisticsVos = storeMapper.selectEChartsData();
        return statisticsVos;
    }

    //分页展示仓库列表
    @Override
    public Result selectsStoreListByPage(Store store, Page page) {
        //step1:查询满足查询条件的所有仓库数量
        Integer count = storeMapper.selectStoreNum(store);
        page.setTotalNum(count);
        //step2:查询当前页要展示的所有仓库列表
        List<Store> stores = storeMapper.selectStoreList(store, page);
        page.setResultList(stores);
        return Result.ok(page);
    }

    //修改仓库信息
    @Override
    public Result updateStore(Store store) {
        //step1:判断修改后的信息是否与已有仓库信息重复  仓库名称不能相同
        Store store1 = storeMapper.selectStoreByStoreId(store);
        if (store1!=null && !store1.getStoreId().equals(store.getStoreId())){
            //说明要修改的仓库名称已经存在，不允许修改
            return Result.err(Result.CODE_ERR_BUSINESS,"修改失败！仓库名称重复...");
        }
        //仓库名称不存在，可以修改
        Integer count = storeMapper.updateStore(store);
        if (count>0){
            //删除缓存，避免数据不一致
            redisTemplate.delete("stores");
            return Result.ok("修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改失败！请联系管理员...");
    }

    //添加仓库时判断仓库编号是否存在
    @Override
    public Result checkStoreNumExists(String storeNum) {
        Store store = storeMapper.selectStoreByStoreNum(storeNum);
//        if (store!=null){
//            return Result.ok()
//        }
//        //要添加的仓库的仓库编号已经存在，不能添加
//        return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！仓库编号已经存在...");
        return Result.ok(store==null);
    }

    //添加仓库
    @Override
    public Result addStore(Store store) {
        //先判断仓库名是否已经存在
        Store store1 = storeMapper.selectStoreByStoreName(store);
        if (store1!=null){
            return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！仓库名重重复...");
        }
        Integer count = storeMapper.addStore(store);
        if (count>0){
            //删除缓存，避免数据不一致
            redisTemplate.delete("stores");
            return Result.ok("仓库添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！请联系管理员...");
    }

    //删除仓库
    @Override
    public Result deleteStoreByStoreId(Integer storeId) {
        Integer count = storeMapper.deleteStoreByStoreId(storeId);
        if (count>0){
            //删除缓存，避免数据不一致
            redisTemplate.delete("stores");
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！请联系管理员...");
    }
}
